<?php
namespace App\Http\Controllers\Api\Mall;

use App\Http\Controllers\Controller;
use App\Products;
use App\ProductsType;
use Illuminate\Http\Request;

class ProductsController extends Controller
{
    /**
     * @function 商品列表
     *
     * @param Request $request
     * @return $this
     */
    public function productList(Request $request)
    {
        $type = $request->input('type', 0);
        //商品加上下架功能
        if ($type > 0) {
            $query = ProductsType::find($type)->Products()->orderBy('id', 'desc')->where('on_sale', 1);
        } else {
            $query = Products::orderBy('sort', 'desc')->where('on_sale', 1);
        }
        $Products = $query->paginate(10);

        return response()->json(['code' => 1000, 'data' => $Products])->withCallback($request->input('callback'));
    }

    /**
     * @function 商品详情
     *
     * @param Request $request
     * @return $this
     */ 
    public function productDetail(Request $request)
    {
        $Products = Products::findOrFail($request->input('id'));

        return response()->json(['code' => 1000, 'data' => $Products])->withCallback($request->input('callback'));
    }
}